/*
 * VIPResource.cpp
 *
 *  Created on: 2014. 3. 7.
 *      Author: the2
 */

#include "VIPResource.h"
#include <Database/Static.h>
#include "../NUtil/Pool.h"

namespace ProjectZ { namespace NResource {

VIPSkillBuffData::~VIPSkillBuffData()
{
	Clear();
}

void
VIPSkillBuffData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsFlyTree.clear();
}

bool VIPSkillBuffData::LoadResource()
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery(" SELECT * FROM resource_vip_skill_buff");

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		VIPSkillBuffData::Flyweight * pFly = new VIPSkillBuffData::Flyweight;

		int vipType = 0;
		clsAuto->GetRecord() >> NATIONAL_CODE >> vipType
				>> pFly->_EXP_CONST >> pFly->_ITEM_DROP_CONST
				>> pFly->_SKILL1_TID >> pFly->_SKILL1_LEVEL >> pFly->_SKILL1_VALUE
				>> pFly->_SKILL2_TID >> pFly->_SKILL2_LEVEL >> pFly->_SKILL2_VALUE;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[vipType] = pFly;
	}

	return true;
}

VIPSkillBuffData::Flyweight * VIPSkillBuffData::GetFly(int vipType)
{
	auto iter = clsFlyTree.find(vipType);

	if ( iter == clsFlyTree.end() )
		return NULL;
	else
		return iter->second;
}


VIPResource::VIPResource()
{
	pVipSkillBuffData = new VIPSkillBuffData;
}

VIPResource::~VIPResource()
{
	delete pVipSkillBuffData;
}

bool
VIPResource::LoadResource()
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery(" SELECT * FROM resource_vip");

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		clsAuto->GetRecord() >> NATIONAL_CODE
				>> _data._VIP1_VP >> _data._VIP2_VP
				>> _data._VIP3_VP >> _data._MAX_VP
				>> _data._MINUS_VP_MIN_PERIOD >> _data._MINUS_VP_AMOUNT_PER_PERIOD
				;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}
	}

	return pVipSkillBuffData->LoadResource();
}

VIPSkillBuffData::Flyweight *
VIPResource::GetFly(int vipType)
{
	return pVipSkillBuffData->GetFly(vipType);
}

} /* namespace NResource */ } /* namespace ProjectZ */
